<?php
$PATH = 'bonsai/common';
require_once "$PATH/file/File.php";
require_once "$PATH/file/Image.php";
require_once "$PATH/util/Collection.php";

$PATH = 'cLanguage';
require_once "$PATH/model/entities/Language.php";
require_once "$PATH/model/dao/LanguageDAO.php";

$PATH = 'cPanel';
require_once "$PATH/controller/CPanelController.php";

$PATH = 'cAttachment';
require_once "$PATH/model/entities/Attachment.php";
require_once "$PATH/model/entities/AttachDetail.php";
require_once "$PATH/model/dao/AttachmentDAO.php";
require_once "$PATH/model/dao/AttachDetailDAO.php";
require_once "$PATH/business/AttachBusiness.php";
require_once "$PATH/controller/AttachController.php";

$PATH = 'cNews';
require_once "$PATH/model/entities/NewsCategory.php";
require_once "$PATH/model/entities/News.php";
require_once "$PATH/model/dao/NewsCategoryDAO.php";
require_once "$PATH/model/dao/NewsDAO.php";
require_once "$PATH/business/NewsBusiness.php";

/**
 * Enter description here ...
 * @author NgoAnhTu
 * @date 2013-11-02
 */
final class NewsController extends AttachController {
	
	const VIEW_PATH = 'cNews/view/';
	const TYPE = 'News';
	const NAME = 'news';
	const PICS_ITEMS = 5;
	
	/**
	 * @var NewsBusiness
	 */
	private $newsBusiness;
	
	/**
	 * Constructor
	 */
	public function NewsController () {
		parent::AttachController(self::TYPE, self::NAME);
		$this->newsBusiness = new NewsBusiness();
	}
	
	/**
	 * (non-PHPdoc)
	 * @see CPanelController::checkSecurity()
	 */
	protected function checkSecurity() {
// 		if (!isset($_SESSION[Dispatcher::USER_SESSION])) {
// 			$this->redirect('user', 'login');
// 		}
	}

	/**
	 * Action: View all NewsCategory
	 * @action(cgrid)
	 */
	public function cgrid($page = 1, $pattern = '', $ids = '') {
		$MSG_CONFIRM = str_replace(LG::ENTITY, LG::NEWSCAT, LG::MSG_DEL_CONFIRM);
		if (!HTTP::isGet()) {
			$this->newsBusiness->activeCaterories($ids);
			$this->msg(self::NAME, 'cgrid', '', str_replace(LG::ENTITY, LG::NEWSCATS, LG::MSG_UPDATE_STATUS), 2);
		} 
		$cats = $this->newsBusiness->searchCategories($this->get(self::LANGUAGE), $page = 1, $pattern = '');
		require_once self::VIEW_PATH . 'cgrid.html';
	}
	
	/**
	 * Action: Delete NewsCategory
	 * @action(cdel) 
	 */
	public function cdel($id) {
		$msg = '';
		$cat = $this->newsBusiness->findCatById($id);
	
		if ($cat == null) {
			$msg = str_replace(LG::ENTITY, LG::NEWSCAT, LG::MSG_NOT_EXIST);
			$msg = str_replace(LG::VAL, $id, $msg);
		} else {
			if ($this->newsBusiness->cdel($id) != null) {
				$msg = str_replace(LG::ENTITY, LG::NEWSCAT, LG::MSG_DELETED);
				$msg = str_replace(LG::VAL, $cat->name, $msg);
			} else {
				$msg = str_replace(LG::ENTITY, LG::NEWSCAT, LG::MSG_NOT_DEL);
				$msg = str_replace(LG::VAL, $cat->name, $msg);
			}
		}
		
		$this->msg(self::NAME, 'cgrid', '', $msg);
	}
	
	/**
	 * Action: Create new NewsCategory
	 * @action(ccreate)
	 */
	public function ccreate($name) {
		$msg = '';
		
		if (!HTTP::isGet()) {
			$name = trim($name);
			
			if (!$this->newsBusiness->isCategoryExist($this->get(self::LANGUAGE), $name)) {
				$cat = $this->newsBusiness->createCategory($this->get(self::LANGUAGE), $name);
				if ($cat != null) {
					$msg = str_replace(LG::ENTITY, LG::NEWSCAT, LG::MSG_SAVE);
					$msg = str_replace(LG::VAL, $name, $msg);
				} else {
					$msg = str_replace(LG::ENTITY, LG::NEWSCAT, LG::MSG_NOT_SAVE);
					$msg = str_replace(LG::VAL, $name, $msg);
				}
				$this->msg(self::NAME, 'cgrid', '', $msg);
			} else {
				$msg = str_replace(LG::ENTITY, LG::NEWSCAT, LG::MSG_EXIST);
				$msg = str_replace(LG::VAL, $name, $msg);
			}
		}
		
		require_once self::VIEW_PATH . 'ccreate.html';
	}

	/**
	 * Action: Edit NewsCategory
	 * @action(cedit) 
	 */
	public function cedit($id, $name) {
		$lang = $this->get(self::LANGUAGE);
		$name = trim($name);
		$msg = '';
		
		$cat = $this->getCategory($id);
		
		if (!HTTP::isGet()) {
			if (!$this->newsBusiness->isExistOther($id, $name, $lang)) {
				$cat->name = $name;
				$cat = $this->newsBusiness->updateCategory($cat);
				if ($cat != null) {
					$msg = str_replace(LG::ENTITY, LG::NEWSCAT, LG::MSG_SAVE);
					$msg = str_replace(LG::VAL, $name, $msg);
				} else {
					$msg = str_replace(LG::ENTITY, LG::NEWSCAT, LG::MSG_NOT_SAVE);
					$msg = str_replace(LG::VAL, $name, $msg);
				}
				$this->msg(self::NAME, 'cgrid', '', $msg);
			} else {
				$msg = str_replace(LG::ENTITY, LG::NEWSCAT, LG::MSG_EXIST);
				$msg = str_replace(LG::VAL, $name, $msg);
			}
		}
		
		require_once self::VIEW_PATH . 'cedit.html';
	}

	/**
	 * Action: View all News
	 * @action(ngrid)
	 */
	public function ngrid($page = 1, $pattern = '', $cid = 0, $ids = '', $submit = '') {
		$MSG_CONFIRM = str_replace(LG::ENTITY, LG::NEWS, LG::MSG_DEL_CONFIRM);
		$cat = $this->getCategory($cid);

		if (!HTTP::isGet() && $submit == LG::F_SAVE) {
			$this->newsBusiness->active($ids);
			$this->msg(self::NAME, 'ngrid', "cid=$cid", str_replace(LG::ENTITY, LG::NEWS, LG::MSG_UPDATE_STATUS), 2);
		} 
		$result = $this->newsBusiness->searchNews($cid, $page, $pattern);
		require_once self::VIEW_PATH . 'ngrid.html';
	}

	/**
	 * Action: Create new News
	 * @action(ncreate)
	 */
	public function ncreate($cid, $title, $header, $body, $author) {
		$msg = '';
		$cat = $this->getCategory($cid);		
		
		if (!HTTP::isGet()) {
			$title = trim($title);
			
			if (!$this->newsBusiness->isExist($title)) {
				$cat = $this->newsBusiness->create($cid, $title, $header, $body, $author);
				if ($cat != null) {
					$msg = str_replace(LG::ENTITY, LG::NEWS, LG::MSG_SAVE);
					$msg = str_replace(LG::VAL, $title, $msg);
				} else {
					$msg = str_replace(LG::ENTITY, LG::NEWS, LG::MSG_NOT_SAVE);
					$msg = str_replace(LG::VAL, $title, $msg);
				}
				$this->msg(self::NAME, 'ngrid', "cid=$cid", $msg);
			} else {
				$msg = str_replace(LG::ENTITY, LG::NEWS, LG::MSG_EXIST);
				$msg = str_replace(LG::VAL, $title, $msg);
			}
		}
		
		require_once self::VIEW_PATH . 'ncreate.html';
	}
	
	private function getCategory($cid) {
		$cat = $this->newsBusiness->findCatById($cid);
		if ($cat == null || $cat->lang != $this->get(self::LANGUAGE)) {
			$msg = str_replace(LG::ENTITY, LG::NEWSCAT, LG::MSG_NOT_EXIST);
			$msg = str_replace(LG::VAL, $cid, $msg);
			$this->msg(self::NAME, 'cgrid', '', $msg);
		}
		return $cat;
	}
	
}
?>